// 点餐结算页面的优惠结算逻辑
// 原价
let prices = 0;
// 实际付款
let payments = 0;
// 消费卡抵扣金额
let Actualamounts = 0;
export default {
	computed: {
		// 核心计算
		calculationamount() {
			console.log('进入计算');
			// 该计算中所输出的打印均为出问题之后方便调试勿删
			/**
			 * @param {already_deduction_amount} 属性是为了构建逻辑计算展示该商品实际抵扣的金额
			 * @param {deduction_amount} 需要提交给后台的单个商品优化价格
			 * @param {Discountarrcondition} 是否显示余额不足提示
			 * @param {max_deduction} 每个商品的最大优惠价格
			 * @param {is_deduction} 单个商品是否优惠
			 * @param {FormData['is_deduction']} 整个订单是否有优惠
			 * @param {FormData['deduction_amount_total']} 整个订单的优惠价格
			 * */
			if (this['FormInfor']['products'] <= 0) return {
				// 原价
				prices,
				// 实际付款
				payments,
				// 消费卡抵扣金额
				Actualamounts,
			};
			let amount = this['amount'];
			console.log(this['FormInfor']['products'],'products');
			// 取出当前勾选的商品
			let Deductionarr = this['FormInfor']['products'].filter(item => {
				if (item['is_deduction']) {
					return item
				}
			});
			// 取出当前未勾选的商品
			let noDeductionarr = this['FormInfor']['products'].filter(item => {
				if (!item['is_deduction']) {
					// 将未选择优惠的商品的实际抵扣价格补回余额中
					amount += item['deduction_amount'];
					// 将实际补扣优惠归零
					item['deduction_amount'] = 0;
					return item
				}
			});

			// 勾选商品计算逻辑
			for (var i = 0; i < Deductionarr['length']; i++) {
				let item = Deductionarr[i];
				let max_deduction = (item['max_deduction'] * item['num']);
				// item['is_deduction'] = 1;
				console.log('当前选择优惠商品', item);
				console.log('当前余额', amount)
				/**
				 * @如果当前商品的实际抵扣金额大于等于最大优惠金额则代表已经计算过了直接跳过当前循环进入下一个循环
				 * */
				if (item['deduction_amount'] >= max_deduction) {
					console.log('当前选择优惠商品优惠条件满足跳出');
					console.log(`当前选择优惠商品实际抵扣金额${item['deduction_amount']}`);
					console.log(`当前选择优惠商品展示优惠金额${item['already_deduction_amount']}`);
					item['Discountarrcondition'] = false;
					continue;
				};
				/**
				 * @如果当前商品的实际抵扣金额小于最大优惠金额则代表当前商品为余额不足状态
				 * */
				if (item['deduction_amount'] < max_deduction) {
					console.log('当前选择优惠商品为不足抵扣状态');
					console.log(`当前商品实际抵扣金额${item['deduction_amount']}`);
					/**
					 * @最大抵扣金额减去已经抵扣的金额得到需要补的优惠金额
					 * */
					let repair = max_deduction - item['deduction_amount'];

					console.log(`当前商品需补优惠金额${repair}`);
					if (amount >= repair) {
						console.log('当前余额大于需要补的优惠金额');
						item['deduction_amount'] += repair;
						item['Discountarrcondition'] = false;
						amount -= repair;

					} else if (amount < repair) {
						console.log('当前余额小于于需要补的优惠金额');
						item['deduction_amount'] += amount;
						item['Discountarrcondition'] = true;
						amount = 0;
					}
					// 展示金额最后计算完毕之后再设置,一切以deduction_amount实际抵扣金额为标准
					item['already_deduction_amount'] = item['deduction_amount'];
					console.log(`当前商品补完优惠金额之后剩余余额${amount}`);
					console.log(`当前商品补完优惠金额之后的实际补贴金额${item['deduction_amount']}`);
					console.log(`当前商品补完优惠金额之后的展示补贴金额${item['already_deduction_amount']}`);
				}
			};

			// 未勾选商品计算逻辑
			for (var i = 0; i < noDeductionarr['length']; i++) {
				let item = noDeductionarr[i];
				let max_deduction = (item['max_deduction'] * item['num']);
				console.log('当前未选择优惠商品', item);
				console.log('当前余额', amount)
				/**
				 * @剩余余额足够抵扣当前商品则展示可抵扣金额
				 * */
				if (amount >= max_deduction) {
					console.log(`当前余额足够抵扣未勾选优惠的第${i}个商品`);
					item['already_deduction_amount'] = max_deduction;
					// 隐藏余额不足提示
					item['Discountarrcondition'] = false;
				} else {
					console.log(`当前余额不足够抵扣未勾选优惠的第${i}个商品`);
					// 剩余余额不足够抵扣当前商品则将剩余余额补到展示抵扣上
					item['already_deduction_amount'] = amount;
					item['Discountarrcondition'] = true;
				}
				console.log(
					`当前未勾选优惠的第${i}个商品的展示优惠${item['already_deduction_amount']}和实际抵扣优惠${item['deduction_amount']}`);
			}
			// 计算消费卡抵扣
			Actualamounts = this['FormInfor']['products'].map(item => {
				if (item['is_deduction']) {
					return item['deduction_amount'];
				}
				return 0
			}).reduce((total, currentValue) => {
				return total + currentValue;
			});
			// 计算原价
			prices = this['FormInfor']['products'].map(item => {
				return item['adj_pay'] * item['num']
				// if(this['BrandInfor']['brand_id'] == 10 || this['BrandInfor']['brand_id'] == 2){
				// 	;
				// }else{
				// 	return item['pay_price'] * item['num'];
				// }
			}).reduce((total, currentValue) => {
				return total + currentValue;
			});
			this['amount'] = amount;
			this['FormInfor']['is_deduction'] = Deductionarr['length'] > 0 ? 1 : 0;
			this['FormInfor']['deduction_amount_total'] = Actualamounts;
			payments = prices - Actualamounts;
			return {
				// 原价
				prices,
				// 实际付款
				payments,
				// 消费卡抵扣金额
				Actualamounts,
			};

		},
		// 计算佣金
		commission() {
			try {
				let _m = this['FormInfor']['products'].map(item => {
					return item['self_commission'] * item['num']
				}).reduce((total, currentValue) => {
					return total + currentValue;
				});
				return this.RetainDecimalPoints({
					num:String(_m),
					toFixed:4,
					iscarry:false
				})
				 
			} catch (e) {
				return 0
			}
		},
	}
}
